package com.mowanka.mokeng.app.base

import android.content.res.Resources
import android.os.Bundle
import androidx.databinding.ViewDataBinding
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.ScreenUtils
import com.mowanka.mokeng.app.ext.dismissLoadingExt
import com.mowanka.mokeng.app.ext.showLoadingExt
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

/**
 * 时间　: 2019/12/21
 * 作者　: hegaojian
 * 描述　: 你项目中的Activity基类，在这里实现显示弹窗，吐司，还有加入自己的需求操作 ，如果不想用Databind，请继承
 * BaseVmActivity例如
 * abstract class BaseActivity<VM : BaseViewModel> : BaseVmActivity<VM>() {
 */
abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDbActivity<VM, DB>() {

    abstract override fun initView(savedInstanceState: Bundle?)

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    private val h = ScreenUtils.getScreenHeight()
    private val w = ScreenUtils.getScreenWidth()
    private val moreOrLess = (maxOf(h, w) * 9) <= (minOf(h, w) * 16) // true 长边适配 ， false 短边适配
    private val widthOrHeight = (h > w) xor moreOrLess
    private val adaptFunc = if (widthOrHeight) AdaptScreenUtils::adaptWidth else AdaptScreenUtils::adaptHeight
    private val adaptSize = if (moreOrLess) 667 else 375

    override fun getResources(): Resources {
        return adaptFunc(super.getResources(), adaptSize)
    }
}